package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-29
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @PostMapping("saveCourse")
    public R saveCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id=courseService.saveCourse(courseInfoVo);
        return R.ok().data("courseId",id);
    }
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("selectCoursePublish/{courseId}")
    public R selectCoursePublishVoById(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.selectCoursePublishVoById(courseId);
        return R.ok().data("coursePublishVo", coursePublishVo);
    }
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse course=new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }

    /**
     * 分页查询
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @PostMapping("pageQueryForCondition/{current}/{limit}")
    public R pageQueryForCondition(
            @PathVariable("current") long current,
            @PathVariable("limit") long limit, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> p=new Page<>(current,limit);
        courseService.pageQueryForCondition(p,courseQuery);
        List<EduCourse> records = p.getRecords();
        long total = p.getTotal();
        return R.ok().data("list",records).data("total",total);
    }
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        Boolean bool=courseService.deleteCourseInfo(courseId);
        if(bool){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     *   远程调用，生成课程信息，组成订单的一部分
     * @param courseId 课程Id
     * @return
     */
    @GetMapping("getCourse/{courseId}")
    public EduCourse getCourse(@PathVariable String courseId){
        EduCourse course= courseService.getById(courseId);
        return course;
    }
}

